<?php
namespace Admin\Controller;
use Admin\Logic\EvaluationCatLogic;

class EvaluationController extends BaseController {

    public function categoryList(){

        $EvaluationCat = new EvaluationCatLogic();
        $cat_list = $EvaluationCat->evaluation_cat_list(0, 0, false,'',$_GET['id']);
        $this->assign('cat_list',$cat_list);
        $this->assign('id',$_GET['id']);
        $this->display('categoryList');
    }

    public function category(){
        $EvaluationCat = new EvaluationCatLogic();
 		$act = I('GET.act','add');
        $this->assign('act',$act);
        $cat_id = I('GET.cat_id');

        $cat_info = array();
        if(!empty($cat_id)){

            $cat_info = D('evaluation_cat')->where('cat_id='.$cat_id)->find();

            $this->assign('cat_info',$cat_info);
        }
        $cats = $EvaluationCat->evaluation_cat_list(0,$cat_info['parent_id'],true,'',$_GET['cart_id']);
        $this->assign('cat_select',$cats);
        $this->assign('category_id',$_GET['cart_id']);
        $this->display();
    }

    public function categoryHandle(){
        $data = I('post.');

        if($data['act'] == 'add'){
            if(empty($data['deduct_money'])){
                $data['deduct_money'] = 0;
            }
            if($data['deduct_money'] < 0){
                $this->error("扣除金额必须大于0",U('Admin/Evaluation/categoryList',array('id'=>$data['category_id'])));
            }
            $d = D('evaluation_cat')->add($data);
            $model = new EvaluationCatLogic();
            $model->refresh_cat($d);
                   $category_id=$_POST['category_id'];
        }

        if($data['act'] == 'edit')
        {
            if ($data['cat_id'] == $data['parent_id'])
            {
                $this->error("所选分类的上级分类不能是当前分类",U('Admin/Evaluation/category',array('cat_id'=>$data['cat_id'])));
            }
            $EvaluationCat = new EvaluationCatLogic();
            $children = array_keys($EvaluationCat->evaluation_cat_list($data['cat_id'], 0, false,'',$_POST['category_id'])); // 获得当前分类的所有下级分类
            if (in_array($data['parent_id'], $children))
            {
                $this->error("所选分类的上级分类不能是当前分类的子分类",U('Admin/Evaluation/category',array('cat_id'=>$data['cat_id'])));
            }
            $d = M('evaluation_cat')->where("cat_id=$data[cat_id]")->save($data);
            $category_id=$_POST['category_id'];
        }

        if($data['act'] == 'del'){
            $res = M('evaluation_cat')->where('parent_id ='.$data['cat_id'])->select();
            if ($res) {
                exit(json_encode('还有子分类，不能删除'));
            }
            //先查询大分类id
            $find=M('evaluation_cat')->where('cat_id='.$data['cat_id'])->field('category_id')->find();

            $category_id=$find['category_id'];
            $r = M('evaluation_cat')->where('cat_id='.$data['cat_id'])->delete();
            if($r) exit(json_encode(1));
        }
        if($d){
            $this->success("操作成功",U('Admin/Evaluation/categoryList',array('id'=>$category_id)));
        }else{
            $this->error("操作失败",U('Admin/Evaluation/categoryList',array('id'=>$category_id)));
        }
    }

    public function evaluationList(){
        $Evaluation =  M('evaluation');
        $res = $list = array();
        $p = empty($_REQUEST['p']) ? 1 : $_REQUEST['p'];
        $size = empty($_REQUEST['size']) ? 20 : $_REQUEST['size'];

        $where = " 1 = 1 ";
        $t_name = trim(I('t_name'));
        $t_name && $where.=" and t_name like '%$t_name%' ";
        $mobile = trim(I('mobile'));
        $mobile && $where.=" and mobile like '%$mobile%' ";


        $res = $Evaluation->where($where)->order('add_time desc')->page("$p,$size")->select();
        $count = $Evaluation->where($where)->count();// 查询满足要求的总记录数
        $pager = new \Think\Page($count,$size);// 实例化分页类 传入总记录数和每页显示的记录数
        $page = $pager->show();//分页显示输出

        $do_type_name_arr = array("1"=>"去门店","2"=>"约上门");
        if($res){
        	foreach ($res as $val){
        		$val['add_time'] = date('Y-m-d H:i:s',$val['add_time']);
                $val['do_type_name'] = $do_type_name_arr[ $val['do_type'] ];
        		$list[] = $val;
        	}
        }
        $this->assign('t_name',$t_name);// 赋值数据集
        $this->assign('mobile',$mobile);// 赋值数据集
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$page);// 赋值分页输出
        $this->display('evaluationList');
    }

    public function evaluation(){
 		$act = I('GET.act','add');
        $info = array();
        $info['publish_time'] = time()+3600*24;
        if(I('GET.evaluation_id')){
            $evaluation_id = I('GET.evaluation_id');
            $info = M('evaluation')->where('evaluation_id='.$evaluation_id)->find();

            //重新赋值
            $info['add_time'] = date('Y-m-d H:i:s',$info['add_time']);

            $do_type_name_arr = array("1"=>"去门店","2"=>"约上门");
            $info['do_type_name'] = $do_type_name_arr[ $info['do_type'] ];

            $info['evaluation_name_arr'] = explode("|||",$info['evaluation_name_text']);

            $info['evaluation_money_arr'] = explode("|||",$info['evaluation_money_text']);


        }

        $this->assign('act',$act);
        $this->assign('info',$info);

        $this->display();
    }

    public function evaluationHandle(){
        $data = I('post.');
        $data['publish_time'] = strtotime($data['publish_time']);
        //$data['content'] = htmlspecialchars(stripslashes($_POST['content']));
        if($data['act'] == 'add'){
        	$data['add_time'] = time();
            $r = M('evaluation')->add($data);
        }

        if($data['act'] == 'edit'){
            $r = M('evaluation')->where('evaluation_id='.$data['evaluation_id'])->save($data);
        }

        if($data['act'] == 'del'){
        	$r = M('evaluation')->where('evaluation_id='.$data['evaluation_id'])->delete();
        	if($r) exit(json_encode(1));
        }
        $referurl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : U('Admin/Evaluation/evaluationList');
        if($r){
            $this->success("操作成功",$referurl);
        }else{
            $this->error("操作失败",$referurl);
        }
    }
}
